<template>
  <div class="myDialog">
    <!-- 弹窗组件 -->
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="title"
      :before-close="closeDialog"
      :close-on-click-modal="false"
      width="550px">
      <el-form
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :disabled="isDisabled"
        label-width="100px"

      >
        <el-form-item :label="$t('version')" prop="version">
          <el-input v-model="form.version"/>
        </el-form-item>
        <el-form-item :label="$t('addAppPackage')" prop="filePath">
          <el-upload
            :headers="headers"
            :action="baseApi + '/config/file/UploadApk'"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
            :limit="1"
            :file-list="fileList"
            multiple
            accept=".apk">
            <el-button type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item :label="$t('updateContent')" prop="content">
          <el-input v-model="form.content" rows="10" type="textarea" />
        </el-form-item>
      </el-form>
      <div v-if="!isDisabled" slot="footer" class="dialog-footer">
        <el-button icon="el-icon-circle-close" @click="closeDialog">{{
          $t("cancel")
        }}</el-button>
        <el-button v-btnThrottle="doSave" type="primary" icon="el-icon-receiving">{{
          $t("save")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import crudJob from '@/api/system/appupdate'
import mySetting from '@/mixins/mySetting'
import networkTips from '@/mixins/networkTips'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'
export default {
  name: 'MyDialog',
  mixins: [mySetting, networkTips],
  props: {
    isDisabled: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      headers: {
        Token: getToken()
      },
      form: {},
      filePath: '',
      fileList: [],
      dialogFormVisible: false,
      rules: {
        version: [
          { required: true, message: this.$t('versionRequired'), trigger: 'blur' }
        ],
        filePath: [
          { required: true, message: this.$t('filePathRequired'), trigger: 'blur' }
        ]
      } // 表单校验规则
    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  methods: {
    uploadSuccess(file) {
      this.form.filePath = file.data
    },
    beforeUpload(file) {
      const isLt30M = file.size / 1024 / 1024 < 30
      if (!isLt30M) {
        this.$message.error('上传安装包大小不能超过 30MB!')
      }
      return isLt30M
    },
    // 新增数据
    doSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.form.filePath == null || this.form.filePath === '') {
          // ???????
        }
        crudJob.appUpload(this.form).then(res => {
          // this.doMerge(res)
          this.netTips(res, this.closeDialog, this.handleUpdate)
        })
      })
    },
    // 重置dialog表单信息
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 打开弹窗
    openDialog(val) {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    },
    // 关闭弹窗
    closeDialog() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    handleUpdate() {
      this.$emit('initList')
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
